<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use ZigaStrgar\Orderable\Orderable;
use Intervention\Image\ImageManagerStatic as Image;

/**
 * App\Workshop
 *
 * @property int $id
 * @property string|null $title 厂房标题
 * @property int|null $rent 租金 单位25元/㎡/月
 * @property int|null $workshop_property_type_id 物业类型id
 * @property int|null $area 建筑面积 平方米
 * @property string|null $tag 标签 逗号分隔
 * @property string|null $address 厂房地址
 * @property int|null $built_area 建筑面积 平方米
 * @property float|null $high 首层层高
 * @property float|null $elevator 电梯类型 T
 * @property float|null $distribution 配电类型 kv
 * @property string|null $code 厂房编号
 * @property int|null $workshop_industry_id 厂房行业id
 * @property string|null $desc 厂房描述
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereBuiltArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereDistribution($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereElevator($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereHigh($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereRent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereTag($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereWorkshopIndustryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereWorkshopPropertyTypeId($value)
 * @mixin \Eloquent
 * @property int|null $floor 楼层
 * @property string|null $owndesc 户主描述
 * @property int|null $park_id 园区id
 * @property string|null $ownertel 业主电话
 * @property string|null $owner 业主名称
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereFloor($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereOwndesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereOwner($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereOwnertel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereParkId($value)
 * @property int|null $area_id 面积所属id
 * @property string|null $logo 厂区图片
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereAreaId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereLogo($value)
 * @property int|null $price 价格  5000元/平方米
 * @property int|null $type 类型：0为出租，1为出售
 * @property string|null $slider 轮播图
 * @property string|null $latitude 经度
 * @property string|null $longitude 维度
 * @property-read \App\WorkshopIndustry|null $industry
 * @property-read \App\Park|null $park
 * @property-read \App\WorkshopPropertyType|null $propertyType
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereLatitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereLongitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereSlider($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereType($value)
 * @property string|null $p_province_id
 * @property string|null $p_city_id
 * @property string|null $p_district_id
 * @property string|null $payratio
 * @property string|null $matching_id
 * @property string|null $property
 * @property int|null $house_type
 * @property int|null $is_recommend 是否推荐
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereHouseType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereIsRecommend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereMatchingId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop wherePCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop wherePDistrictId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop wherePProvinceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop wherePayratio($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereProperty($value)
 * @property int|null $click_count 点击量
 * @property int $call_count 电话次数
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereCallCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereClickCount($value)
 * @property string|null $is_out
 * @property string|null $owner_release
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereIsOut($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereOwnerRelease($value)
 */
class Workshop extends Model
{
    use Orderable;

    public function orderable(){
        return [
            'is_out'=> 'ASC',
            'created_at' => 'DESC',
            'id'
        ];
    }
    public static function boot()
    {
        parent::boot();
        static::saving(function ($model) {
            if($model->logo==''){
                $arr = explode(',',$model->slider);
                if(count($arr)>=1 && substr($model->slider,0,1)==','){
                    $model->logo = $arr[1];
                }else{
                    $model->logo = $arr[0];
                }
            }
            $area = $model->area;
            $area_id = 8;
            if($area<2000){
                $area_id = 1;
            }else if($area<5000){
                $area_id = 2;
            }else if($area<8000){
                $area_id = 3;
            }else if($area<12000){
                $area_id = 4;
            }else if($area<16000){
                $area_id = 5;
            }else if($area<20000){
                $area_id = 6;
            }else if($area<30000){
                $area_id = 7;
            }
            $model->area_id = $area_id;

        });
    }
    //
    protected $table="workshop";
    public function getLogoAttribute($value)
    {
        return $value;
    }
    //行业
    public function industry(){
        return $this->belongsTo(WorkshopIndustry::class,'workshop_industry_id');
    }
    public function propertyType(){
        return $this->belongsTo(WorkshopPropertyType::class,'workshop_property_type_id');
    }
    public function park(){

        return $this->belongsTo(Park::class,'park_id');
    }


    public function getWorkshopIndustryIdAttribute($value)
    {
        return explode(',', $value);
    }

    public function setWorkshopIndustryIdAttribute($value)
    {
        $this->attributes['workshop_industry_id'] = implode(',', $value);
    }

    public function getMatchingIdAttribute($value)
    {
        return explode(',', $value);
    }

    public function setMatchingIdAttribute($value)
    {
        $this->attributes['matching_id'] = implode(',', $value);
    }
    public static function getSelectOptions()
    {
        $options = \DB::table('workshop')->select('id','title as text')->get();
        $selectOption = [];
        foreach ($options as $option){
            $selectOption[$option->id] = $option->text;
        }
        return $selectOption;
    }
}
